<template>
 <div class="tiezi-content">
       <Top title="我的收藏"  iconShow='1'></Top>
       <!-- 这个是 顶部的切换栏-->
       <div class="tab">
          <ul>
              <li>
                  <a href="./collectProduct.html">产品</a>
              </li>
              <li>
                  <a href="javascript:void(0)" class="select">资讯</a>
                  <div class="showborder"></div>
              </li>
              <li><a href="./collectTieZi.html">帖子</a></li>
          </ul>
       </div>
       <!-- 这个是编辑 -->
      <div class="tiezi-content-edit" @click="goEdit">{{edit?"取消":"编辑"}}</div>
       <!-- 注意这里是加下拉组件的 -->
    <mt-loadmore :top-method="loadTop" ref="loadmore" @top-status-change="handleTopChange">
      <div class="tiezi-content-item" v-for="(item,index) in selectedList" :key="index">
        <div v-if="edit" class="tiezi-content-item-check">
          <!-- <input type="checkbox" ref="checkbox" :cId="item.visitId" @change="singleSelect"> -->
          <input type="checkbox" :id="item.visitId" class="chk_1" ref="checkbox" @change="singleSelect" :cId="item.visitId" />
          <label :for="item.visitId"></label>
        </div>
        <div class="tiezi-content-item-left" @click="goArtical(item.informationId)">
          <img :src="item.img" />
        </div>
        <div class="tiezi-content-item-right" @click="goArtical(item.informationId)">
           <div class="tiezi-content-item-right-title">{{item.title}}</div>
          <p class="tiezi-content-item-right-detail">{{item.detail}}</p>
          <p class="tiezi-content-item-right-time">{{item.create_time | ymd}}</p>
        </div>
      </div>
  </mt-loadmore>
    <!-- 顶部的固定删除功能 -->
    <div class="deleteDo" v-if="edit">
      <div class="deleteDo-left">
        <label for="checkAll">
          <input type="checkbox" id="checkbox_a1" class="chk_1 checkall" ref="allSelect" @change="allSelect" />
          <label for="checkbox_a1"></label>
        </label>
        <span style="position:absolute;">全选</span>
        <span>{{selectNum}}/{{selectedList.length}}</span>
      </div>
      <div class="deleteDo-right" @click="shanchu">删除</div>
    </div>
    <!-- 如果一个产品没有，那么空的图片则显示 -->
   <div class="showing" v-if="selectedList.length==0">
          <img src="../../../assets/nothing.png" alt="">
    </div>
    <loading title="加载中..."  v-if="loadMoreShow"></loading>
     <Nomore v-if="noMoreShow"></Nomore>
  </div>
</template>

<script>
import Vue from 'vue'
import '../../../js/lib/flexble.js'
import commons from '../../../common/js/common.js'
import Top from 'components/Header/Header'
import Foot from 'components/Footer/Footer'
import { Indicator, MessageBox,Toast } from "mint-ui";
export default {
  name: 'collectProduct',
  components: { Top,Foot },
  data() {
    return {
          loadMoreShow: false,
          topStatus: '',
          pageNum: 1,  //表示的是下拉分页的累加器
          selectedList: [],
          flag: true,
          noMoreShow: false,
          loadMoreShow: false,
          arr: [],
          idStr: '',
          edit: false,
          selectNum: 0
    }
  },
  mounted() {
    Vue.http.interceptors.push((request, next) => { //请求拦截器
      //请求前
      request.headers.set('token', commons.getCookie('token')); //设置请求头
      Indicator.open('加载中...'); //请求loading
      next((response) => {
        //请求后
        Indicator.close(); //隐藏loading
        return response
      })
    })
    var token = commons.getCookie("token");
    // 获取访问的资讯列表
      this.zixun(token);
  },
  filters:{
      ymd(val){
         return commons.ymd(val);
      }
  },
   created() {
        var _this = this;
        this.$nextTick(function() {
            window.addEventListener('scroll', onScroll)
        })
        // 判断滚动条位置，加载更多
        function onScroll() {
            var scrollTop = document.body.scrollTop;
            var innerHeight = window.innerHeight;
            var offsetHeight = document.body.offsetHeight;
            if (scrollTop + innerHeight >= offsetHeight) {
                if (_this.flag) {
                    if (_this.selectedList.length >= 7) {
                        _this.flag = false;
                        _this.getList()
                    }
                }
            }
        }
    },
  methods: {
    handleTopChange: function (status) {  //注意这个是下拉组件中的方法
        this.topStatus = status;
    },
    // 上拉加载数据
    getList() {
        this.pageNum += 1;
        this.loadMoreShow = true;
        var token = commons.getCookie("token");
        var _this = this;
        setTimeout(function() {
            _this.$http.get("/loansupermarket-app/collect/getCustomInformationVisit?pageNum=" + _this.pageNum + "&&pageSize=7" + '&visitType=' + 1 + "&token=" + token)
                .then((response) => {
                    var resBody = response.body;
                    if (resBody.code == '000') {
                        _this.flag = true;
                        _this.loadMoreShow = false;
                        if (resBody.data.length < 7) {
                            _this.noMoreShow = true;
                            _this.flag = false;
                        } else {
                            _this.flag = true;
                        }
                        _this.selectedList = _this.selectedList.concat(resBody.data);
                      
                    } else if(resBody.code == '444') {
                          window.location.href="../user/login.html?history=1";
                    }
                })
        }, 500)
    },
    loadTop() {  // 下拉刷新数据的操作   
            setTimeout(()=>{
              this.zixun();
              this.$refs.loadmore.onTopLoaded();
            },500);
    },
    zixun(){
       this.noMoreShow = false; 
           var token =commons.getCookie("token");
            // 加载第一页数据并实例化
            let _this=this;
              this.$http.get("/loansupermarket-app/collect/getCustomInformationVisit?pageNum=" +1 + "&&pageSize=7" + '&visitType=' + 1 + "&token=" + token)
              .then((response) => {
                    var resBody = response.body;
                    if (resBody.code == '000') {
                       this.selectedList.splice(0,resBody.data.length); //下拉请求新的数据，并且将第一页的数据删掉
                      this.selectedList = resBody.data.concat(this.selectedList); //表示的是将最新的数据插在最前面
                      
                        if (this.selectedList.length < 7) {
                            this.flag = false;
                            this.noMoreShow = true; 
                        }else {
                                this.flag = true;
                            }
                            _this.selectedList.forEach(function(item, index) {
                            })
                    }else if(resBody.code == '444'){
                          window.location.href="../user/login.html?history=1";
                    }else{
                        MessageBox.alert(resBody.mesg);
                    }
                })
    },
    goEdit() {
      this.edit = !this.edit;
      if (!this.edit) {
        this.selectNum = 0;
      }
    },
    singleSelect() {
      var _this = this;
      var flag = true;
      this.$refs.checkbox.forEach(function(item, index) {
        if (!item.checked) {
          _this.$refs.allSelect.checked = false;
          flag = false;
          return false;
        }
        if (flag) {
          _this.$refs.allSelect.checked = true;
        }
      })
      // 判断已选列表个数
      this.judgeNum();
    },
    // 判断已选列表个数
    judgeNum() {
      var _this = this;
      _this.selectNum = 0;
      this.$refs.checkbox.forEach(function(item, index) {
        if (item.checked) {
          _this.selectNum += 1
        }
      })
    },
    allSelect() {
      // 如果是全选，那么所有的checkbox全部变成已选
      var _this = this;
      if (this.$refs.allSelect.checked) {
        this.$refs.checkbox.forEach(function(item, index) {
          item.checked = true;
        })
      } else {
        this.$refs.checkbox.forEach(function(item, index) {
          item.checked = false;
        })
      }
      // 判断已选列表个数
      this.judgeNum();
    },
    shanchu() {
      var _this = this;
      var token =commons.getCookie("token");
      // 添加了一个属性cId，根据属性获取产品id再添加到数组提交到后台
      this.$refs.checkbox.forEach(function(item, index) {
        if (item.checked) {
          _this.arr.push(item.getAttribute('cId'));
          _this.idStr = _this.arr.join(',');
        }
      })
      if (this.idStr.length == 0) {
        Toast('请勾选你要删除的帖子~');
      } else {
        this.$http.post('/loansupermarket-app/collect/deleteInformationVisit', { visitType: 1, idStr: this.idStr, token: token }).then(function(response) {
          if (response.body.code == '000') {
            Toast('删除成功！');
            setTimeout(function() {
              window.location.reload()
            }, 500)
          } else if (response.body.code == '444') {
              window.location.href="../user/login.html?history=1";
          } else {
            MessageBox.alert(response.body.mesg);
          }
        })
      }
    },
    // 进入精选的详情页
   goArtical(id) { 
       window.location.href="../news/article.html?id="+id;
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../scss/base.scss';
//这个是tab栏的样式
.tab{
   border-bottom: 1px solid #e4e4e4;
      margin-top: 1.2rem;
      position: fixed!important;
      width: 100%;
      top: 0;
      background: #fff;
      z-index: 3;
      height: 1.2rem;
      line-height: 1.2rem;
      display: flex;
    >ul{
      display:flex;
       width: 100%;
      height: 100%;
      box-sizing: border-box;
      li{   
        font-size:14px;
         color: #666;
        font-weight: bold;
        position:relative; 
          flex:1;
          text-align:center;
          &:after{
            content:'';
            position:absolute;
            width:1px;
            height:rem(50);
            background: #e4e4e4;
            right:0px;
            top:50%;
            margin-top:rem(-25);
          }
           a{
            display: block;
            color: #666;
          }
           .select{
                 color: rgb(48, 204, 219);
             }
      }
      .showborder{
        position:absolute;
        width:rem(150);
        height:1px;
        background:rgb(48, 204, 219);
        bottom:-1px;
        left:50%;
        margin-left:rem(-75);
      }
    }
}
.showing{
  margin-top: rem(120);
}
.tiezi-content {
  text-align: center;
  margin-top: 2.9rem;
  width: 100%;
  box-sizing: border-box;
  .weui-loadmore_line {
    margin-top: 0.6rem;
  }
  &-edit {
    position: fixed;
    right: 0.4rem;
    top: 0.35rem;
    z-index: 999;
    color: #fff;
    font-size: 0.4rem;
  }
  &-item {
    background: #fff;
    box-sizing: border-box;
    padding: 0rem 0.4rem;
    padding-top: 0.4rem;
    width: 100%;
    height: 3.17rem;
    display: flex;
    &-check {
      width: 1rem;
      input {
        margin-top: 1rem;
        width: 0.4rem;
        height: 0.4rem;
      }
    }
    &-left {
      width: 3.6rem;
      height: 100%;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-right {
      width: 5.32rem;
      height: 100%;
      margin-left: 0.27rem;
      text-align: left;
      border-bottom: 1px solid $borderColor;
      position: relative;

      &-title {
        margin-bottom: 2px;
        font-size: 0.4rem;
        height: 0.5rem;
        overflow : hidden;
        text-overflow: ellipsis;
       display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      word-break: break-all; /* 追加这一行代码 */
        @include textOverflow(1);
        font-weight: 600;
        color: $titleColor;
      }
      &-detail{
        height: 1.5rem;
        line-height: .6rem;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
      }
    }
  }
  .deleteDo {
     z-index:1000;
    display: flex;
    width: 100%;
    position: fixed;
    left: 0;
    height: 1.3rem; // line-height: 1.3rem;
    color: #fff;
    bottom: 0;
    &-left {
      flex: 6;
      background: rgba(0, 0, 0, 0.4);
      border-right: 1px solid #fff;
      text-align: left;
      box-sizing: border-box;
      
      padding: 0 0.4rem;
      .chk_1+label {
        margin-top: 0.35rem;
        background-color: #FFF;
        border: 1px solid #C1CACA;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
        padding: 0.2rem;
        border-radius: 50%;
        display: inline-block;
        position: relative;
        margin-right: 30px; // display: none;
        // vertical-align: -15%;
        width: 0.1rem;
        height: 0.1rem;
      }
      .chk_1:checked+label {
        margin-top: 0.35rem;
        background-color: $primaryColor;
        border: 1px solid $primaryColor;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
        color: #243441;
      }

      span {
        float: right;
        line-height: 1.3rem;
        font-size: 0.3rem;
      }
    }
    &-right {
      font-size: 0.3rem;
      flex: 3;
      line-height: 1.3rem;
      background: rgba(0, 0, 0, 0.4)
    }
  }

  .chk_1 {

    display: none; // vertical-align: -15%;
    width: 0.3rem;
    height: 0.3rem;
  }
  .chk_1+label {
    color:#fff !important;
    margin-top: 1.1rem;
    background-color: #FFF;
    border: 1px solid #C1CACA;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05),
    inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    padding: 0.2rem;
    border-radius: 50%;
    display: inline-block;
    position: relative;
    margin-right: 15px; // display: none;
    // vertical-align: -15%;
    width: 0.1rem;
    height: 0.1rem; // &:nth-last-of-type() {
    //   margin-top: 0.8rem;
    //   background-color: #FFF;
    //   border: 1px solid #C1CACA;
    //   box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
    //   padding: 9px;
    //   border-radius: 50%;
    //   display: inline-block;
    //   position: relative;
    //   margin-right: 15px; // display: none;
    //   // vertical-align: -15%;
    //   width: 0.1rem;
    //   height: 0.1rem;
    // }
  }
  .chk_1+label:active {

    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px 1px 3px rgba(0, 0, 0, 0.1);
  }
  .chk_1:checked+label {
    font-size: 0.17rem;
    margin-top: 1.1rem;
    background-color: $primaryColor;
    border: 1px solid $primaryColor;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
    color: #243441;
  }
  .chk_1:checked+label:after {
    content: '\2714'; //勾选符号 
    position: absolute;
    top: 0px;
    left: 0px;
    color: #fff;
    width: 100%;
    text-align: center;
    font-size: 1.4em; // padding: 1px 0 0 0;
    vertical-align: text-top;
  }
}
</style>
